package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.model.AppRegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.kenuo.domain.PoloSmsLog;
import com.ruoyi.kenuo.service.IPoloSmsLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@Api(value = "系统注册相关",tags = "系统注册相关")
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IPoloSmsLogService poloSmsLogService;


    @Autowired
    private RedisCache redisCache;

    @ApiOperation("注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "sysFlag", value = "系统标识：1：电脑  2：手机  3：设备", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "snCode", value = "SN", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "saleNel", value = "销售通道 0家庭版，1机构版", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "nickName", value = "姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "telCode", value = "手机验证码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "roleIds", value = "多个角色id，数组类型", required = true, dataType = "List")
    })
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) throws Exception {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String mobile = user.getUsername();
        String code = user.getTelCode();
        if(StringUtils.isNotEmpty(mobile)&&StringUtils.isNotEmpty(code)){
            PoloSmsLog poloSmsLog = new PoloSmsLog();
            poloSmsLog.setUserPhone(mobile);
            poloSmsLog.setMobileCode(code);
            Boolean bl = poloSmsLogService.checkCurCode(poloSmsLog);
            if(!bl){
                return AjaxResult.error("验证码不正确！");
            }
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /***
     * app端注册接口
     * @param user
     * @return
     * @throws Exception
     */

    @PostMapping("/appRegister")
    public AjaxResult appRegister(@RequestBody AppRegisterBody user) throws Exception {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        //手机号码 当做用户名
        String mobile = user.getUsername();
        //邮箱验证码
        String verCode = user.getVerCode();
        //手机验证码
        String code = user.getTelCode();
        if(StringUtils.isNotEmpty(mobile)&&StringUtils.isNotEmpty(code)){
            PoloSmsLog poloSmsLog = new PoloSmsLog();
            poloSmsLog.setUserPhone(mobile);
            poloSmsLog.setMobileCode(code);
            Boolean bl = poloSmsLogService.checkCurCode(poloSmsLog);
            if(!bl){
                return AjaxResult.error("验证码不正确！");
            }
        }
        if(StringUtils.isNotEmpty(mobile)&&StringUtils.isNotEmpty(verCode)){
           String vCode = redisCache.getCacheObject(mobile);
           if(StringUtils.isEmpty(vCode)||!verCode.equals(vCode)){
               return AjaxResult.error("验证码不正确或过期！");
           }
        }
        String msg = registerService.appRegister(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

}
